package com.chinawlx.wlxteacher.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.utils.WLXActivityManagerUtil;
import com.chinawlx.wlxteacher.utils.WLXConstant;
import com.chinawlx.wlxteacher.utils.WLXUIUtils;
import com.chinawlx.wlxteacher.widget.WLXCustomToast;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者:黄帅
 * 时间:16/9/6
 * 描述:滤镜效果的activity
 */
public class WLXFilterActivity extends WLXBaseActivity {
    @BindView(R.id.iv_show_image)
    ImageView mIvShowImage;
//    @BindView(R.id.Template)
//    RecyclerView mRecyclerView;

    //    private WLXFilterGalleryAdapter mAdapter;
    private Bitmap mBitmap;
    private String mImagePath;
    private String mFrom = "";
    private Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WLXActivityManagerUtil.addActivity(this);
        mContext = this;
        setContentView(R.layout.activity_filter);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        initPassValue();
//        initAdapter();
        initView();
    }


    private void initPassValue() {

        Intent intent = getIntent();
        mImagePath = intent.getStringExtra(WLXConstant.IMAGE_PATH);
        mFrom = intent.getStringExtra("from");

        try {
            mBitmap = WLXUIUtils.getBitmap(mImagePath);
            //这里拿到的是缩略图
//            mBitmap = WLXImageUtils.getThumbnail(mImagePath,5);
        } catch (Exception e) {
            e.printStackTrace();
            onBackPressed();
            return;
        }
        if (mBitmap == null) onBackPressed();
    }

//    private void initAdapter() {
//        //设置布局管理器
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//        mRecyclerView.setLayoutManager(linearLayoutManager);
//        //设置适配器
//        mAdapter = new WLXFilterGalleryAdapter(getApplicationContext(), mBitmap);
//        mRecyclerView.setAdapter(mAdapter);
//        mAdapter.setListener(new WLXFilterGalleryAdapter.TemplateImageClickListener() {
//            @Override
//            public void onClick(Bitmap bitmap) {
//                mIvShowImage.setImageBitmap(bitmap);
//                mBitmap = bitmap;
//            }
//        });
//    }

    private void initView() {
        mIvShowImage.setImageBitmap(mBitmap);
    }


    /**
     * 点击了确定
     */
    public void thisOk(View view) {
        if (mFrom != null && mFrom.equals(WLXRecordVideoPhotoActivity.class.getSimpleName())) {
            Intent intent = new Intent(mContext, WLXSendPictureActivity.class);
            intent.putExtra(WLXConstant.IMAGE_PATH,mImagePath);
            intent.putExtra("from",WLXFilterActivity.class.getSimpleName());
            mContext.startActivity(intent);
            finish();
        } else {
            boolean saveOk = WLXUIUtils.saveBitmap2file(mBitmap, mImagePath);
            if (saveOk) {
                Intent intent = new Intent();
                intent.putExtra(WLXConstant.IMAGE_PATH, mImagePath);
                setResult(RESULT_OK, intent);
                finish();
            } else {
                WLXCustomToast.show("保存失败,请重试!");
            }
        }
    }

    public void backMain(View view) {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        //释放内存中bitmap的资源
        if (mFrom != null && mFrom.equals(WLXRecordVideoPhotoActivity.class.getSimpleName())) {
            // TODO: 16/9/13 返回录视频

        } else {
            setResult(RESULT_CANCELED);
        }

        finish();
    }
}
